﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01_OOP
{
    /// <summary>
    /// 集合
    /// </summary>
    internal static class CollectionExample
    {
        /// <summary>
        /// yield
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static IEnumerable<int> GetRandomNumberList(int count)
        {
            for (int i = 0; i < count; i++)
            {
                yield return Random.Shared.Next();
            }
        }

        /// <summary>
        /// IEnumerable的应用1
        /// </summary>
        public static void ForEach()
        {
            var array = new object[]
            {
                1,
                "aff",
                new char[]
                {
                    'a', 'w'
                },
                new List<string>()
                {
                    "jack",
                    "tom"
                }
            };

            foreach (var item in array)
            {
                if (item is IEnumerable && item is not string)
                {
                    foreach (var iitem in (IEnumerable)item)
                    {
                        Console.WriteLine(iitem);
                    }
                }
                else
                {
                    Console.WriteLine(item);
                }
            }
        }
        /// <summary>
        /// IEnumerable的linq应用
        /// Enumerable类对IEnumerable进行了原型扩展，提供了大量的
        /// </summary>
        public static void TestLinq()
        {
            var list = new int[] { 1, 2, 3, 5, 8, 7, 78, 45, 12 };
            list.Any(a => a > 20);//判断是否存在大于5的
            list.Select(s => new int?(s));//投影，用于集合元素的转变
            list.Where(a => a > 50);//过滤
            //返回一个类似Dictionary<int,IEnumerable<int>>的数据结构。
            list.GroupBy(a => a % 2);//根据奇偶性进行分组
        }
        /// <summary>
        /// 实现枚举器
        /// </summary>
        public class RandomNumber : IEnumerable<int>
        {
            private int[] list;

            public RandomNumber(int count)
            {
                list = new int[count];
                for (int i = 0; i < count; i++)
                {
                    var item = Random.Shared.Next();
                    list[i] = item;
                }
            }

            public IEnumerator<int> GetEnumerator()
            {
                return new Enumerator(this);
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }

            /// <summary>
            /// 实现迭代器，建议使用内部类，摆脱私有性的限制。
            /// </summary>
            class Enumerator : IEnumerator<int>
            {
                private readonly RandomNumber obj;
                private int _index = -1;
                public Enumerator(RandomNumber obj)
                {
                    this.obj = obj;
                }
                public int Current => obj.list[_index];

                object IEnumerator.Current => Current;

                public void Dispose()
                {

                }

                public bool MoveNext()
                {
                    //迭代器一般用内部类
                    //内部内可以访问到外部类的私有成员
                    if (_index < obj.list.Length)
                    {
                        _index++;
                        return true;
                    }
                    return false;
                }

                public void Reset()
                {
                    _index = -1;
                }
            }
        }

        class MyCollection : IEnumerable<object>
        {
            private IEnumerable<object> _targt;

            public MyCollection(IEnumerable<object> targt)
            {
                _targt = targt;
            }

            public IEnumerator<object> GetEnumerator()
            {
                return _targt.GetEnumerator();
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return ((IEnumerable)_targt).GetEnumerator();
            }
        }
    }
}
